/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ForumScreen.java
 *
 * Created on 23/04/2011, 18:00:24
 */
package implementation.gui.screens;

import java.util.Collection;
import java.util.Iterator;

import javax.swing.JOptionPane;

import implementation.client.EUserParam;
import implementation.client.IUserInput.EUserAction;
import implementation.client.IUserInput;

import implementation.shared.stubs.IThreadStub;


/**
 *
 * @author Maxim
 */
public class ForumScreen extends javax.swing.JFrame {

	
	private  IUserInput _userOutputInput;
	private Object _monitor;

	private int _selectedThreadId;

	private String[] _threadTopics;
	private int[] _threadsIDs;
   
	
	/** Creates new form ForumScreen */
    public ForumScreen(IUserInput userOutputInput, Object monitor, Collection<IThreadStub> threads) {
        
    	this._selectedThreadId = -1;
    	this._userOutputInput = userOutputInput;
    	this._monitor = monitor;  
    	
    	int tLoopCounter = 0;
    	
    	this._threadsIDs = new int[threads.size()];
    	this._threadTopics = new String[threads.size()];
    	
        for(Iterator<IThreadStub> iter = threads.iterator(); iter.hasNext();){
        	IThreadStub tThreadStub = iter.next();
        	
        	this._threadTopics[tLoopCounter] = tThreadStub.get_threadTopic();
        	this._threadsIDs[tLoopCounter] = tThreadStub.get_threadID();
        	tLoopCounter++;
        }
    	
    	
    	
    	
    	
    	
    	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        threads = new javax.swing.JList(this._threadTopics);
        jLabel1 = new javax.swing.JLabel();
        showThread = new javax.swing.JButton();
        profile = new javax.swing.JButton();
        logout = new javax.swing.JButton();
        addThread = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        back = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(450, 350));


        threads.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                threadsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(threads);

        jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        jLabel1.setText("Threads Topics:");

        showThread.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        showThread.setText("Show thread");
        showThread.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showThreadActionPerformed(evt);
            }
        });

        profile.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        profile.setText("Show profile");
        profile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileActionPerformed(evt);
            }
        });

        logout.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        logout.setText("Logout");
        logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutActionPerformed(evt);
            }
        });

        addThread.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        addThread.setText("Add Thread");
        addThread.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addThreadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(addThread)
                                    .addComponent(showThread))
                                .addGap(17, 17, 17))
                            .addComponent(logout, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(profile, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(showThread)
                        .addGap(18, 18, 18)
                        .addComponent(addThread)
                        .addGap(18, 18, 18)
                        .addComponent(profile)
                        .addGap(14, 14, 14)
                        .addComponent(logout))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tempus Sans ITC", 0, 14)); // NOI18N
        jLabel2.setText("Malda Community:");

        back.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        back.setText("Back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        exit.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(back)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(exit))
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(back)
                    .addComponent(exit))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void showThreadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showThreadActionPerformed
        
        if(this._selectedThreadId == -1){
            JOptionPane.showMessageDialog(null, "No thread selected! ", null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        this._userOutputInput.setSelectedAction(EUserAction.EGetThread);
        this._userOutputInput.setParam(EUserParam.EID, this._selectedThreadId);
        
        
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
}//GEN-LAST:event_showThreadActionPerformed

    private void profileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileActionPerformed
        
        
        this._userOutputInput.setSelectedAction(EUserAction.EProfile);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
    }//GEN-LAST:event_profileActionPerformed

    private void logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutActionPerformed
        
        
        this._userOutputInput.setSelectedAction(EUserAction.ELogout);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
        
    }//GEN-LAST:event_logoutActionPerformed

    private void threadsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_threadsValueChanged
    	
    	this._selectedThreadId = this._threadsIDs[this.threads.getSelectedIndex()];
    
    }//GEN-LAST:event_threadsValueChanged

    private void addThreadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addThreadActionPerformed
        
    	this._userOutputInput.setSelectedAction(EUserAction.EAddThread);
    	
    	synchronized(this._monitor){
    		this._monitor.notify();
    	}    	
    	this.dispose();
    }//GEN-LAST:event_addThreadActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed

    	
    	this._userOutputInput.setSelectedAction(EUserAction.EExit);
    	
    	synchronized(this._monitor){
    		this._monitor.notify();
    	}    	
    	this.dispose();
    	
    }//GEN-LAST:event_exitActionPerformed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
    	
    	this._userOutputInput.setSelectedAction(EUserAction.EBack);
    	
    	synchronized(this._monitor){
    		this._monitor.notify();
    	}    	
    	this.dispose();
    }//GEN-LAST:event_backActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addThread;
    private javax.swing.JButton back;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton logout;
    private javax.swing.JButton profile;
    private javax.swing.JButton showThread;
    private javax.swing.JList threads;
    // End of variables declaration//GEN-END:variables
}
